﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Control_ChiTietSanPham : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            XuLy xl = new XuLy();
            string query = "";
            if (Request.QueryString["MaSP"] != null)
            {
               // query = "select sp.MaSP, sp.TenSP, sp.GiaBanSP, sp.HinhSP, sp.MoTaSP, sp.NgayNhapSP, sp.SoLuongSP, k.TenKieu, cl.TenChatLieu  from SanPham sp, Kieu k, ChatLieu cl where sp.MaChatLieu=cl.MaChatLieu and sp.MaKieu=k.MaKieu and sp.MaSP'"+ Request.QueryString["MaSP"].ToString() + "'";
               query = "select * from SanPham where MaSP='" + Request.QueryString["MaSP"].ToString() + "'";
            }
            else
            {
                query = "select * from SanPham";
            }
            DataList1.DataSource = xl.Load(query);
            DataList1.DataBind();
        }
    }
    XuLy xl = new XuLy();

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "chonmua")
        {
            if (Session["DangNhap"] != null)
            {
                DataTable sanpham = new DataTable();
                DataTable giohang = new DataTable();
                sanpham = xl.Load("select * from SanPham where MaSP='" + e.CommandArgument.ToString() + "'");
                giohang = (DataTable)Session["GioHang"];
                giohang.DefaultView.Sort = "MaSP";
                int vt = giohang.DefaultView.Find(e.CommandArgument);
                if (vt >= 0)
                {
                    //xuat thong bao co san pham trong gio hang
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", "alert('san pham co rui')", true);
                }
                else
                {
                    DataRow d;
                    d = giohang.NewRow();
                    d["TenDN"] = Session["DangNhap"].ToString();
                    d["MaSP"] = e.CommandArgument.ToString();
                    d["TenSp"] = sanpham.Rows[0]["TenSP"];
                    d["Gia"] = sanpham.Rows[0]["GiaBanSP"];
                    d["SoLuong"] = 1;
                    d["ThanhTien"] = d["Gia"];
                    d["NgayDat"] = DateTime.Now;
                    giohang.Rows.Add(d);
                }
                giohang.AcceptChanges();
                Session["GioHang"] = giohang;
                Session["tamgiohang"] = "flag";
            }
            // neu chua dang nhap 
            else
            {
                DataTable sp = new DataTable();
                DataTable ghchuadn = new DataTable();
                sp = xl.Load("select * from SanPham where MaSP='" + e.CommandArgument.ToString() + "'");
                ghchuadn = (DataTable)Session["GioHangChuaDangNhap"];
                ghchuadn.DefaultView.Sort = "MaSP";
                int vt = ghchuadn.DefaultView.Find(e.CommandArgument);
                if (vt >= 0)
                {
                    // xuat thong bao da co san pham 
                }
                else
                {
                    DataRow dr;
                    dr = ghchuadn.NewRow();
                    dr["MaSP"] = e.CommandArgument.ToString();
                    dr["TenSp"] = sp.Rows[0]["TenSP"];
                    dr["Gia"] = sp.Rows[0]["GiaBanSP"];
                    dr["SoLuong"] = 1;
                    dr["ThanhTien"] = dr["Gia"];
                    dr["NgayDat"] = DateTime.Now;
                    ghchuadn.Rows.Add(dr);
                }
                ghchuadn.AcceptChanges();
                Session["GioHangChuaDangNhap"] = ghchuadn;
                Session["tamgiohangchuadn"] = "flag";
            }

            Response.Redirect("~/TrangChu.aspx");
        }
    }
}